package com.qdxwx.manage.controller.picture;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.member.MemberTypeService;
import com.qdxwx.data.service.picture.PictureAlbumService;
import com.qdxwx.data.service.subject.SubjectService;
import com.qdxwx.data.service.website.WebsiteProfileService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.category.Category;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.manager.Manager;
import com.qdxwx.models.picture.PictureAlbum;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 图片专辑Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/picture/album")
public class PictureAlbumEditController extends BaseController {

    @Autowired
    private PictureAlbumService pictureAlbumService;
    @Autowired
    private WebsiteProfileService websiteProfileService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private MemberTypeService memberTypeService;

    @GetMapping("/add.html")
    public String add(HttpServletRequest request, ModelMap mmap) {
        Manager manager = SessionUtils.getManager();
        if (manager == null) return error404;
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        if (channelId < 1) return error404;
        mmap.put("channelId", channelId);
        mmap.put("memberTypes", memberTypeService.getList());
        return "picture/picture-album-add";
    }

    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) return error404;
        PictureAlbum pictureAlbum = pictureAlbumService.get(id);
        if (pictureAlbum == null) return error500;
        mmap.put("pictureAlbum", pictureAlbum);
        return "picture/picture-album-edit";
    }

    @PostMapping("/save.json")
    @Permissions("picture:album:edit")
    @Log(title = "保存图片专辑", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult save(HttpServletRequest request) {
        Manager manager = SessionUtils.getManager();
        if (manager == null) return error("未登录，禁止操作");
        int id = TKConvert.toInteger(request.getParameter("id"));
        PictureAlbum pictureAlbum = pictureAlbumService.get(id);
        if (pictureAlbum == null) return error("找不到id为:" + id + "的专辑");
        String title = request.getParameter("title");
        if (TKString.isEmpty(title)) return error("名称不能为空");
        String badWord = websiteProfileService.checkBadWord(title);
        if (badWord != null) {
            return error("书名含有敏感词‘" + badWord + "’，请删除后重新提交！");
        }
        int categoryId = TKConvert.toInteger(request.getParameter("categoryId"));
        if (categoryId < 1) return error("请选择分类");
        Category category = categoryService.get(categoryId);
        if (category == null) return error("找不到指定分类");
        String intro = request.getParameter("intro");
        if (TKString.isEmpty(intro)) return error("请输入专辑简介");
        badWord = websiteProfileService.checkBadWord(intro);
        if (badWord != null) {
            return error("简介含有敏感词‘" + badWord + "’，请删除后重新提交！");
        }
        pictureAlbum.setTitle(title);
        pictureAlbum.setTypeId(TKConvert.toInteger(request.getParameter("typeId")));
        pictureAlbum.setCategoryId(category.getCategoryId());
        pictureAlbum.setCategoryName(category.getCategoryName());
        pictureAlbum.setSubjectId(category.getSubjectId());
        pictureAlbum.setIntro(intro);
        pictureAlbum.setIsElite(TKConvert.toInteger(request.getParameter("isElite"), 0));
        pictureAlbum.setIsTop(TKConvert.toInteger(request.getParameter("isTop"), 0));
        pictureAlbum.setViewCount(TKConvert.toInteger(request.getParameter("viewCount"), 0));
        int row = pictureAlbumService.update(pictureAlbum);
        return row > 0 ? AjaxResult.success() : AjaxResult.error("操作失败，请联系管理员！");
    }

    /**
     * 上下架
     */
    @PostMapping("/update/status.json")
    @Log(title = "修改图片状态", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult UpdateStatus(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        int status = TKConvert.toInteger(request.getParameter("status"), 0);
        if (id < 1 || status > 1 || status < -1) return error("参数异常");
        PictureAlbum pictureAlbum = pictureAlbumService.get(id);
        if (pictureAlbum == null) return error("未找到指定id书籍，id=" + id);
        if (status == 1) {
            int count = pictureAlbumService.countItem(pictureAlbum.getId());
            if (count < 1) return error("没有任何图片，请在管理内容添加图片再试");
        }
        pictureAlbum.setStatus(status);
        return toAjax(pictureAlbumService.update(pictureAlbum));
    }

    /**
     * 固顶操作
     */
    @PostMapping("/update/top.json")
    @Log(title = "修改图片固顶", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult top(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        int isTop = TKConvert.toInteger(request.getParameter("isTop"), 0);
        if (id < 1 || isTop > 1 || isTop < 0) return error("参数异常");
        PictureAlbum pictureAlbum = pictureAlbumService.get(id);
        if (pictureAlbum == null) return error("未找到指定id图集，id=" + id);
        pictureAlbum.setIsTop(isTop);
        return toAjax(pictureAlbumService.update(pictureAlbum));
    }

    /**
     * 推荐操作
     */
    @PostMapping("/update/elite.json")
    @Log(title = "修改图片推荐", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult elite(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        int isElite = TKConvert.toInteger(request.getParameter("isElite"), 0);
        if (id < 1 || isElite > 1 || isElite < 0) return error("参数异常");
        PictureAlbum pictureAlbum = pictureAlbumService.get(id);
        if (pictureAlbum == null) return error("未找到指定id图集，id=" + id);
        pictureAlbum.setIsElite(isElite);
        return toAjax(pictureAlbumService.update(pictureAlbum));
    }
}
